package com.trade.action;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.trade.common.util.PaginationSupport;
import com.trade.exception.BusinessException;
import com.trade.model.Goods;
import com.trade.model.ProcurmentContract;
import com.trade.model.Supplier;
import com.trade.service.IGoodsService;
import com.trade.service.IProcurmentContractService;
import com.trade.service.IReposityService;
import com.trade.service.ISupplierSevice;
import com.trade.service.IUserService;

/**
 * @author Lin Yang
 * 
 */
public class ProcurmentAction extends DispatchAction {
    private IUserService userService;
    private IGoodsService goodsService;
    private ISupplierSevice supplierSevice;
    private IProcurmentContractService procurmentContractService;
    private IReposityService reposityService;

    public ActionForward presubmit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("goods", getAllGoods());
        request.setAttribute("suppliers", getAllSuppliers());
        return mapping.findForward("contract");
    }

    public ActionForward queryApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = (String) request.getParameter("name");
        List<ProcurmentContract> contracts = procurmentContractService.getApprovingContractLikeName(name);
        request.setAttribute("contracts", contracts);
        return mapping.findForward("approve");
    }

    public ActionForward queryVerify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = (String) request.getAttribute("name");
        List<ProcurmentContract> contracts = procurmentContractService.getVerifyingContractLikeName(name);
        request.setAttribute("contracts", contracts);
        return mapping.findForward("verify");
    }

    public ActionForward queryContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = (String) request.getAttribute("name");
        List<ProcurmentContract> contracts = procurmentContractService.getContractLikeName(name);
        request.setAttribute("contracts", contracts);
        return mapping.findForward("query");
    }

    public ActionForward presprk(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("goods", getAllGoods());
        request.setAttribute("suppliers", getAllSuppliers());
        List<ProcurmentContract> contracts = procurmentContractService.getContractLikeName(null);
        request.setAttribute("contracts", contracts);
        return mapping.findForward("sprk");
    }

    public ActionForward queryReposity(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String logisticNumber = request.getParameter("name");

        request.setAttribute("goods", getAllGoods());
        request.setAttribute("suppliers", getAllSuppliers());
        List<ProcurmentContract> contracts = procurmentContractService.getContractLikeName(null);
        request.setAttribute("contracts", contracts);
        request.setAttribute("repositys", reposityService.getReposityByLogistic(logisticNumber));
        request.setAttribute("users", userService.getAllUsers());
        return mapping.findForward("queryReposity");
    }

    @SuppressWarnings("unchecked")
    private List<Goods> getAllGoods() {
        PaginationSupport goods = new PaginationSupport(Integer.MAX_VALUE, 1);
        goods = goodsService.getGoodsList(goods);
        return (List<Goods>) goods.getItems();
    }

    private List<Supplier> getAllSuppliers() {
        try {
            return supplierSevice.findAllLike("");
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setGoodsService(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    public void setSupplierSevice(ISupplierSevice supplierSevice) {
        this.supplierSevice = supplierSevice;
    }

    public void setProcurmentContractService(IProcurmentContractService procurmentContractService) {
        this.procurmentContractService = procurmentContractService;
    }

    public void setReposityService(IReposityService reposityService) {
        this.reposityService = reposityService;
    }

}
